package com.dianrui.moonfire.walkbaby;

import android.content.Intent;
import android.os.Bundle;
import android.os.CountDownTimer;
import android.support.v7.app.AlertDialog;
import android.text.method.ScrollingMovementMethod;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

import com.blankj.utilcode.util.SPUtils;
import com.dianrui.moonfire.Constant;
import com.dianrui.moonfire.R;
import com.dianrui.moonfire.activity.BaseActivity;
import com.dianrui.moonfire.event.OpenOrCloseBaby;
import com.dianrui.moonfire.okhttp.XHttpRequest;
import com.dianrui.moonfire.util.StringUtils;
import com.dianrui.moonfire.util.ToastUtil;
import com.dianrui.moonfire.util.Url;
import com.google.gson.JsonObject;

import org.greenrobot.eventbus.EventBus;
import org.json.JSONObject;

import butterknife.BindView;
import butterknife.OnClick;

public class WalkBabyTipsActivity extends BaseActivity {
    private long mRuntimes=0;
    private CountDownTimer timer;


    @BindView(R.id.back)
    ImageView back;
    @BindView(R.id.title)
    TextView title;
    @BindView(R.id.tip_img)
    ImageView tipImg;
    @BindView(R.id.wait)
    Button wait;
    private String mOrderId;


    @Override
    public int getContentViewId() {
        return R.layout.activity_walkbaby_tips;
    }


    @Override
    public void initView() {
        customInit(true);
        title.setText(getString(R.string.walkbabydeponsit_tips));
        getCurrentTime();

    }


    private void getCurrentTime() {
        JsonObject jsonObject = new JsonObject();
        if (!StringUtils.isEmpty(SPUtils.getInstance().getString("member_id"))) {
            jsonObject.addProperty("member_id", SPUtils.getInstance().getString("member_id"));
        }
        XHttpRequest.getInstance().postRequests(Url.BABYPAYRIDING, jsonObject.toString(), new XHttpRequest.XCallBack() {

            @Override
            public void onResponse(String result) {
                if (!StringUtils.isEmpty(result)) {
                    try {
                        final JSONObject jsonObject = new JSONObject(result);
                        if (null != jsonObject && !isFinishing()) {
                            if (Constant.DATASUCCESS.equals(jsonObject.optString("status"))) {
                                countDownTimer((jsonObject.optJSONObject("data").optLong("run_times")*1000));
                                mOrderId=jsonObject.optJSONObject("data").optString("order_id");
                            } else {
                                EventBus.getDefault().post(new OpenOrCloseBaby(false));
                                Intent intent = new Intent(WalkBabyTipsActivity.this, WalkBabyReturnCarActivity.class);
                                Bundle bundle = new Bundle();
                                bundle.putString("order_id", mOrderId);
                                intent.putExtras(bundle);
                                startActivity(intent);
                                finish();

                            }
                        }
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                }
            }

            @Override
            public void failedRequest(String result) {
                ToastUtil.showToast(getString(R.string.net_error));
                finish();
            }
        });
    }

    private void countDownTimer(long run_times) {
        timer = new CountDownTimer(run_times, 1000) {
            @Override
            public void onTick(long millisUntilFinished) {
                wait.setEnabled(false);
                wait.setBackgroundResource(R.drawable.gray_shape);
                wait.setText("等待开锁" + millisUntilFinished / 1000 + "秒");
                if(millisUntilFinished%3==0){
                    getCountDownTimes();
                }

            }

            @Override
            public void onFinish() {
                EventBus.getDefault().post(new OpenOrCloseBaby(false));
                wait.setEnabled(true);
                wait.setText("结束用车");
                wait.setBackgroundResource(R.drawable.yellow_gradient);
            }
        };
        timer.start();
    }

    private void getCountDownTimes() {
        JsonObject jsonObject = new JsonObject();
        if (!StringUtils.isEmpty(SPUtils.getInstance().getString("member_id"))) {
            jsonObject.addProperty("member_id", SPUtils.getInstance().getString("member_id"));
        }
        XHttpRequest.getInstance().postRequests(Url.BABYPAYRIDING, jsonObject.toString(), new XHttpRequest.XCallBack() {

            @Override
            public void onResponse(String result) {
                if (!StringUtils.isEmpty(result)) {
                    try {
                        final JSONObject jsonObject = new JSONObject(result);
                        if (null != jsonObject && !isFinishing()) {
                            if (Constant.DATASUCCESS.equals(jsonObject.optString("status"))) {
                                if (jsonObject.optJSONObject("data").optString("vehicle_status").equals("2")) {
                                    EventBus.getDefault().post(new OpenOrCloseBaby(true));
                                    JumpActivitys(WalkBabyRidingActivity.class);
                                    finish();
                                }
                            } else {
                                EventBus.getDefault().post(new OpenOrCloseBaby(false));
                                Intent intent = new Intent(WalkBabyTipsActivity.this, WalkBabyReturnCarActivity.class);
                                Bundle bundle = new Bundle();
                                bundle.putString("order_id", mOrderId);
                                intent.putExtras(bundle);
                                startActivity(intent);
                                finish();
                            }
                        }
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                }
            }

            @Override
            public void failedRequest(String result) {
                ToastUtil.showToast(getString(R.string.net_error));
                finish();
            }
        });
    }



    private void showRuleDialog(String msg) {
        AlertDialog.Builder builder = new AlertDialog.Builder(WalkBabyTipsActivity.this, R.style.RcDialogs);
        final View view1 = LayoutInflater.from(WalkBabyTipsActivity.this).inflate(R.layout.walkbaby_riding_rule, null);
        final TextView content = view1.findViewById(R.id.content);
        content.setText(msg);
        Button know = view1.findViewById(R.id.know);
        content.setMovementMethod(ScrollingMovementMethod.getInstance());
        final AlertDialog dialog = builder.create();
        dialog.setView(view1);
        dialog.show();
        know.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dialog.dismiss();
            }
        });

    }

    @Override
    public void initData() {

    }

    @OnClick({R.id.back,R.id.wait})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.back:
                finish();
                break;
            case R.id.wait:
                finish();
                break;
            default:
                break;
        }
    }




    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (timer != null) {
            timer.cancel();
            timer=null;
        }
    }
}
